<template>
  <div class="mod-config">
     <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>秒杀活动管理</el-breadcrumb-item>
      <el-breadcrumb-item>秒杀商品</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button
          type="primary"
          @click="addOrUpdateHandle()"
        >新增</el-button>
        <el-button
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
        >批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="id" header-align="center" align="center" label="id"></el-table-column>
      <el-table-column prop="name" header-align="center" align="center" label="场次名称"></el-table-column>
      <el-table-column prop="startTime" header-align="center" align="center" label="每日开始时间"></el-table-column>
      <el-table-column prop="endTime" header-align="center" align="center" label="每日结束时间"></el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="启用状态"></el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="创建时间"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="280" label="操作">
        <template slot-scope="scope">
          <el-button type="success" size="small" @click="relationProduct(scope.row.id)">关联商品</el-button>
         <el-button type="primary" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="danger" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <div>
      <el-dialog
        append-to-body
        :close-on-click-modal="false"
        :visible.sync="visible"
        title="关联秒杀商品"
        width="40%"
      >
      <el-cascader
            v-model="selectedCateKeys"
            :options="cateList"
            :props="cateProps"
             @change="handleChange"
          ></el-cascader>
        <!-- <seckillsku-relation ref="seckillskuRelation" :sessionId="currentId"></seckillsku-relation> -->
         <el-select v-model="selectProid" placeholder="请选择" >
          <el-option
            v-for="item in goodsList"
            :key="item.id"
            :label="item.spuName"
            :value="item.id"
            >
          </el-option>
        </el-select>
         <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" >确定</el-button>
    </span>
      </el-dialog>
    </div>
    </el-card>
  </div>
</template>

<script>
import AddOrUpdate from "./SeckillsessionAddUpdate";
export default {
  data() {
    return {
      dataForm: {
        key: ""
      },
       // 商品分类列表
      cateList: [],
      //   级联选择框的配置对象
      cateProps: {
        expandTrigger: 'hover',
        value:"catId",
        label:'catName',
        children:"children"
      },
        selectProid:'',
      goodsList:[],
      //   级联选择框双向绑定对象
      selectedCateKeys: [],
      dataList: [
        {
          id:'1',
            name:"上午专场",
            status:"上线",
            startTime:"2022-4-1 8:00:00",
            endTime:"2022-4-1 12:00:00",
            createTime:"2022-4-1 6:42:14",
        },
        {
          id:'2',
            name:"下午专场",
            status:"上线",
            startTime:"2022-4-1 12:00:00",
            endTime:"2022-4-1 18:00:00",
            createTime:"2022-4-1 10:35:43",
        },
        ],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 2,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      visible: false,
      currentId: 0
    };
  },
  computed:{
     // 当前选中的二级分类Id
    getCateId () {
      if (this.selectedCateKeys.length === 2) {
        return this.selectedCateKeys[1]
      }
      return null
    },
  },
  components: {
    AddOrUpdate,
    // SeckillskuRelation
  },
  created() {
    this.getDataList();
    this.getCateList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
    
    },
     // 根据获取对应的商品列表
    async getGoodsList () {
      // console.log("getCateId",this.getCateId)
      const {data:res} = await this.$http.get(`/xuanmall/spumessage/getAllShopByCatalogId?catalogId=${this.getCateId}&page=1&limit=300`)
      // console.log(res)
      this.goodsList =[],
       res.page.list.forEach(value =>{
         var data = {};
         data.id = value.id
         data.spuName = value.spuName
         data.images = value.images[0]
         data.price = value.price
         data.weight = value.weight
         data.stockCount = value.stockCount
         data.publishStatus = value.publishStatus
         this.goodsList.push(data)
       })
      //  console.log(this.goodsList)
    },
    //   获取所有的商品分类列表
    async getCateList () {
      const {data:res} = await this.$http.get("/xuanmall/category/list/tree")
        if(res.code === 0){
         this.cateList = this.formatData(res.data)
        }
    },
    // 格式化数据，递归将空的children置为undefined
    formatData(data) {
      for (var i = 0; i < data.length; i++) {
        if (data[i].children.length < 1) {
          data[i].children = undefined
        } else {
          this.formatData(data[i].children)
        }
      }
      return data
      },
    getRealtionDataList() {},
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 级联选择框 选中变化 触发
    async  handleChange () {
      this.getGoodsList()
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    relationProduct(id) {
      this.visible = true;
      this.currentId = id;
      // this.$nextTick(() => {
      //   this.$refs.seckillskuRelation.getDataList();
      // });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        // this.$http({
        //   url: this.$http.adornUrl("/coupon/seckillsession/delete"),
        //   method: "post",
        //   data: this.$http.adornData(ids, false)
        // }).then(({ data }) => {
        //   if (data && data.code === 0) {
        //     this.$message({
        //       message: "操作成功",
        //       type: "success",
        //       duration: 1500,
        //       onClose: () => {
        //         this.getDataList();
        //       }
        //     });
        //   } else {
        //     this.$message.error(data.msg);
        //   }
        // });
      });
    }
  }
};
</script>